package com.ruicar.afs.cloud.basic.cms.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.dispatch.contract.CmsInterfaceFeign;
import com.ruicar.afs.cloud.basic.common.entity.*;
import com.ruicar.afs.cloud.basic.common.service.*;
import com.ruicar.afs.cloud.common.config.ContractFeginConfig;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.contract.cms.vo.InterfaceInfoVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.AddressTypeEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractFeginParamEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.PersonRoleTypeEnum;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 获取客户详细信息
 */
@Slf4j
@RestController
@RequestMapping("/basicCustDetailInfo")
@AllArgsConstructor
public class IcosInterfaceInfoController {

    private final BasicCustPersonalDetailService basicCustPersonalDetailService;
    private final BasicContractCustRelationService basicContractCustRelationService;
    private final BasicCustBaseInfoService basicCustBaseInfoService;
    private final BasicCarDetailsService basicCarDetailsService;
    private final BasicMainInfoService basicMainInfoService;
    private final BasicCustAddressDetailsService basicCustAddressDetailsService;
    private final BasicChannelInfoService basicChannelInfoService;
    private final CmsInterfaceFeign cmsInterfaceFeign;
    private final ContractFeginConfig contractFeginConfig;

    @ApiOperation(value = "聚合接口根据合同号码查询合同详情")
    @PostMapping("/custDetailInfo4")
    public IResponse<InterfaceInfoVo> custDetailInfo4(@RequestParam("contractNo") String contractNo) {
        InterfaceInfoVo vo = new InterfaceInfoVo();
        BasicMainInfo mainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>query().lambda()
                .eq(BasicMainInfo::getContractNo,contractNo)
        );
        Map<String, String> headers = new HashMap<>();
        headers.put(AfsEnumUtil.key(ContractFeginParamEnum.CASE_CLIENT_SECRET),contractFeginConfig.getCaseClientSecret());
        headers.put(AfsEnumUtil.key(ContractFeginParamEnum.CASE_CLIENT_ID),contractFeginConfig.getCaseClientId());
        headers.put(AfsEnumUtil.key(ContractFeginParamEnum.CONTENT_TYPE), MediaType.APPLICATION_JSON_VALUE);
        if (EmptyUtils.isNotEmpty(mainInfo)) {

            BasicContractCustRelation mainPersonContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                    eq(BasicContractCustRelation::getContractNo, contractNo)
                    .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.MAIN_PERSON));
            BasicContractCustRelation joinPersonContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                    eq(BasicContractCustRelation::getContractNo, contractNo)
                    .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.JOINT_PERSON));
            BasicContractCustRelation guaranteePersonContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>lambdaQuery().
                    eq(BasicContractCustRelation::getContractNo, contractNo)
                    .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.GUARANTEE_PERSON));
            BasicCarDetails basicCarDetails = basicCarDetailsService.getOne(Wrappers.<BasicCarDetails>lambdaQuery()
                    .eq(BasicCarDetails::getContractNo, contractNo)
            );
            BasicCustPersonalDetail mainPersonalDetail = null;
            BasicCustBaseInfo mainCustBaseInfo = null;
            if (EmptyUtils.isNotEmpty(mainPersonContractCustRelation)) {
                mainPersonalDetail = basicCustPersonalDetailService.getOne(Wrappers.<BasicCustPersonalDetail>lambdaQuery().
                        eq(BasicCustPersonalDetail::getCustNo, mainPersonContractCustRelation.getCustNo()));
                mainCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery().
                        eq(BasicCustBaseInfo::getCustNo, mainPersonContractCustRelation.getCustNo()));
                vo.setBorrower(mainCustBaseInfo.getCustName());
                vo.setBorrower_id_card_type(mainCustBaseInfo.getCertType());
                vo.setBorrower_id_card(mainCustBaseInfo.getCertNo());
                vo.setBorrower_phone(mainCustBaseInfo.getPhone());
                vo.setBorrower_sex(mainPersonalDetail.getSex());
                vo.setBorrower_marriage(mainPersonalDetail.getMaritalStatus());
                List<BasicCustAddressDetails> custAddressDetails = basicCustAddressDetailsService.list(Wrappers.<BasicCustAddressDetails>query().lambda()
                        .eq(BasicCustAddressDetails::getCustNo, mainPersonContractCustRelation.getCustNo())
                        .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.LIVE)
                );
                if (EmptyUtils.isNotEmpty(custAddressDetails)) {
                    BasicCustAddressDetails custAddressDetail = custAddressDetails.get(0);
                    vo.setBorrower_address(custAddressDetail.getAddress());
                }
            }
            BasicCustPersonalDetail joinPersonalDetail = null;
            BasicCustBaseInfo joinCustBaseInfo = null;
            if (EmptyUtils.isNotEmpty(joinPersonContractCustRelation)) {
                joinPersonalDetail = basicCustPersonalDetailService.getOne(Wrappers.<BasicCustPersonalDetail>lambdaQuery().
                        eq(BasicCustPersonalDetail::getCustNo, joinPersonContractCustRelation.getCustNo()));
                joinCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery().
                        eq(BasicCustBaseInfo::getCustNo, joinPersonContractCustRelation.getCustNo()));
                vo.setCoborrower(joinCustBaseInfo.getCustName());
                vo.setCoborrower_id_card_type(joinCustBaseInfo.getCertType());
                vo.setCoborrower_id_card(joinCustBaseInfo.getCertNo());
                vo.setCoborrower_phone(joinCustBaseInfo.getPhone());
                vo.setCoborrower_sex(joinPersonalDetail.getSex());
                vo.setCoborrower_relation(joinPersonContractCustRelation.getCustRelation());
                List<BasicCustAddressDetails> custAddressDetails = basicCustAddressDetailsService.list(Wrappers.<BasicCustAddressDetails>query().lambda()
                        .eq(BasicCustAddressDetails::getCustNo, joinPersonContractCustRelation.getCustNo())
                        .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.LIVE)
                );
                if (EmptyUtils.isNotEmpty(custAddressDetails)) {
                    BasicCustAddressDetails custAddressDetail = custAddressDetails.get(0);
                    vo.setCoborrower_address(custAddressDetail.getAddress());
                }
            }
            BasicCustPersonalDetail guaranteeCustPersonalDetail = null;
            BasicCustBaseInfo guaranteeCustBaseInfo = null;
            if (EmptyUtils.isNotEmpty(guaranteePersonContractCustRelation)) {
                guaranteeCustPersonalDetail = basicCustPersonalDetailService.getOne(Wrappers.<BasicCustPersonalDetail>lambdaQuery().
                        eq(BasicCustPersonalDetail::getCustNo, guaranteePersonContractCustRelation.getCustNo()));
                guaranteeCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>lambdaQuery().
                        eq(BasicCustBaseInfo::getCustNo, guaranteePersonContractCustRelation.getCustNo()));
                vo.setGuarantor(guaranteeCustBaseInfo.getCustName());
                vo.setGuarantor_id_card_type(guaranteeCustBaseInfo.getCertType());
                vo.setGuarantor_id_card(guaranteeCustBaseInfo.getCertNo());
                vo.setGuarantor_relation(guaranteePersonContractCustRelation.getCustRelation());
                vo.setGuarantor_phone(guaranteeCustBaseInfo.getPhone());
                vo.setGuarantor_sex(guaranteeCustPersonalDetail.getSex());
                List<BasicCustAddressDetails> custAddressDetails = basicCustAddressDetailsService.list(Wrappers.<BasicCustAddressDetails>query().lambda()
                        .eq(BasicCustAddressDetails::getCustNo, guaranteePersonContractCustRelation.getCustNo())
                        .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.LIVE)
                );
                if (EmptyUtils.isNotEmpty(custAddressDetails)) {
                    BasicCustAddressDetails custAddressDetail = custAddressDetails.get(0);
                    vo.setGuarantor_address(custAddressDetail.getAddress());
                }
            }
            vo.setApplication_no(mainInfo.getApplyNo());
            vo.setApplication_date(EmptyUtils.isNotEmpty(mainInfo.getIntoFirstDate()) ? DateUtil.format(mainInfo.getIntoFirstDate(), "yyyy-MM-dd") : null);
            vo.setApplication_status(mainInfo.getContractStatus().toString());
            List<BasicChannelInfo> list = basicChannelInfoService.list(Wrappers.<BasicChannelInfo>query().lambda()
                    .eq(BasicChannelInfo::getContractNo, contractNo));


            if (EmptyUtils.isNotEmpty(list)) {
                BasicChannelInfo basicChannelInfo = list.get(0);
                vo.setDealer(basicChannelInfo.getChannelFullName());
                vo.setDealer_phone(basicChannelInfo.getContactPhone());
                vo.setDealer_address(basicChannelInfo.getChannelAdminAddress());
            }
            IResponse<String> response = cmsInterfaceFeign.getWitnessInfo(contractNo, headers);
            log.info("-----------------------向boss系统获取操作人（见证人）数据，返回结果[{}]-----------------------",response);
            if(CommonConstants.SUCCESS.equals(response.getCode())){
                vo.setOperator_user(response.getData());
            }
            vo.setAsset_condition(AfsEnumUtil.desc(mainInfo.getBusinessType()));
            if (EmptyUtils.isNotEmpty(basicCarDetails.getCarManufacturer())) {

                vo.setMaker(basicCarDetails.getCarManufacturer());
            }
            if (EmptyUtils.isNotEmpty(basicCarDetails.getBrandName())) {
                vo.setBrand(basicCarDetails.getBrandName());
            }
            if (EmptyUtils.isNotEmpty(basicCarDetails.getModelName())) {
                vo.setModel(basicCarDetails.getModelName());
            }
            if (EmptyUtils.isNotEmpty(basicCarDetails.getCarKind())) {
                vo.setKinetic_parameter(null);
            }
            if (EmptyUtils.isNotEmpty(basicCarDetails.getCarVin())) {
                vo.setVin(basicCarDetails.getCarVin());
            }
            if (EmptyUtils.isNotEmpty(basicCarDetails.getCarColor())) {
                vo.setColor(basicCarDetails.getCarColor());
            }
            if (EmptyUtils.isNotEmpty(basicCarDetails.getSalePrice())) {
                vo.setPrice(basicCarDetails.getSalePrice());
            }

        }else{
            //贷前数据
            IResponse<InterfaceInfoVo> response = cmsInterfaceFeign.getCustomerDetailInfo(contractNo, headers);
            if(CommonConstants.SUCCESS.equals(response.getCode())){
                vo = response.getData();
            }
        }
        return IResponse.success(vo);
    }


}
